﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Negocio;
using Business.Entities;

namespace UI.Web
{
    public partial class Comisiones : System.Web.UI.Page
    {
        ComisionLogic _logic;


        public enum FormModes
        {
            Alta,
            Baja,
            Modificacion
        }

        public FormModes FormMode
        {
            get { return (FormModes)this.ViewState["FormMode"]; }
            set { this.ViewState["FormMode"] = value; }
        }

        private Business.Entities.Comision Entity
        {
            get;
            set;
        }

        private int SelectedID
        {
            get
            {

                if (this.ViewState["SelectedID"] != null)
                {
                    return (int)this.ViewState["SelectedID"];
                }
                else
                {
                    return 0;
                }

            }
            set
            {
                this.ViewState["SelectedID"] = value;
            }
        }

        private bool IsEntitySelected
        {
            get
            {
                return (this.SelectedID != 0);
            }
        }


        private ComisionLogic Logic
        {
            get
            {
                if (_logic == null)
                {
                    _logic = new ComisionLogic();
                }
                return _logic;

            }
        }

        private void LoadGrid()
        {
            this.gvComisiones.DataSource = this.Logic.GetAll();
            this.gvComisiones.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnAceptar.Text = "Aceptar";
            if (!Page.IsPostBack)
            {
                LoadGrid();
            }
            else
            {

            }
        }


        private void LoadForm(int id)
        {

            this.Entity = this.Logic.GetOne(id);
            txtId.Text = Entity.ID.ToString();
            txtDescripcion.Text = Entity.Descripcion;
            txtAño.Text = Entity.Anioespecialidad.ToString();
            txtPlan.Text = Entity.Idplan.ToString();


        }

        protected void gvComisiones_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel1.Visible = false;
            this.SelectedID = (int)this.gvComisiones.SelectedValue;
        }

        protected void btnAceptar_Click(object sender, EventArgs e)
        {
            if (this.FormMode == FormModes.Alta)
            {

                Entity = new Business.Entities.Comision();
                Entity.State = Business.Entities.Comision.States.New;
                _logic = new ComisionLogic();

                Entity.Descripcion = txtDescripcion.Text;
                Entity.Anioespecialidad = Int32.Parse(txtAño.Text);
                Entity.Idplan = Int32.Parse(txtPlan.Text);



                _logic.Save(this.Entity);
                Page.Response.Redirect("Comisiones.aspx");
            }
            else if (this.FormMode == FormModes.Modificacion)
            {
                Entity = this.Logic.GetOne(this.SelectedID);
                Entity.State = Business.Entities.Comision.States.Modified;
                _logic = new ComisionLogic();
                Entity.Descripcion = txtDescripcion.Text;
                Entity.Anioespecialidad = Int32.Parse(txtAño.Text);
                Entity.Idplan = Int32.Parse(txtPlan.Text);
                _logic.Save(this.Entity);
                Page.Response.Redirect("Comisiones.aspx");
            }
            else
            {
                Entity = this.Logic.GetOne(this.SelectedID);
                Entity.State = Business.Entities.Comision.States.Deleted;
                _logic = new ComisionLogic();
                _logic.Save(this.Entity);
                Page.Response.Redirect("Comisiones.aspx");
            }

        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            Page.Response.Redirect("Comisiones.aspx");  
        }

        protected void btnNuevo_Click(object sender, EventArgs e)
        {
            Panel1.Visible = true;
            this.FormMode = FormModes.Alta;
            this.gridActionsMaterias.Visible = false;
        }

        protected void btnEditar_Click(object sender, EventArgs e)
        {
            if (this.IsEntitySelected)
            {
                Panel1.Visible = true;
                this.FormMode = FormModes.Modificacion;
                this.LoadForm(this.SelectedID);
                this.gridActionsMaterias.Visible = false;
            }
        }

        protected void btnEliminar_Click(object sender, EventArgs e)
        {
            if (this.IsEntitySelected)
            {
                Panel1.Visible = true;
                this.FormMode = FormModes.Baja;
                this.LoadForm(this.SelectedID);
                btnAceptar.Text = "Eliminar";
                this.gridActionsMaterias.Visible = false;
            }
        }
    }
}
